/* freeircd welcome.c
 *
 * Iniciado em 03/08/2006
 */

#include <errno.h>
#include "misc.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <stdbool.h>
#include <time.h>
#include <stdarg.h>

#include "types.h"
#include "configuration.h"

#include "debug.h"

void do_welcome(CLIENTS *, long, long, long, MOTD *);
void show_version(CLIENTS *);
void show_lusers(CLIENTS *, long, long, long);
void show_motd(CLIENTS *, MOTD *);
void info(CLIENTS *);

extern void write_sock(CLIENTS *, char *, ...);
extern inline void memerror(void *);

void do_welcome(CLIENTS *client, long users, long ircops, long channels, MOTD *motd)
{
/*
:server 001 nick :Welcome to the Internet Relay Network coiso
:server 002 nick :Your host is server, running version ircd
:server 003 nick :This server was created date at time
:server 004 nick server ircd dgikoswx biklmnoprstv
:server 005 nick MAP SILENCE=15 WHOX WALLCHOPS WALLVOICES USERIP CPRIVMSG CNOTICE MODES=6 MAXCHANNELS=10 MAXBANS=500 :are supported by this server
:server 005 nick NICKLEN=10 TOPICLEN=500 AWAYLEN=500 KICKLEN=500 CHANTYPES=#& PREFIX=(ov)@+ CHANMODES=b,k,l,rimnpst CASEMAPPING=rfc1459 NETWORK=network_name :are supported by this server
*/
        write_sock(client,":%s 001 %s :Welcome to the Internet Relay Network %s\r\n",SERVER_NAME,client->nick,client->nick);
        write_sock(client,":%s 002 %s :Your host is %s, running %s version %s, made by triton and orium\r\n",SERVER_NAME,client->nick,client->md5_ip,
                                                                                   FREEIRCD_NAME,FREEIRCD_VERSION);
        write_sock(client,":%s 003 %s :This server was created %s at %s\r\n",SERVER_NAME,client->nick,__DATE__,__TIME__);
/*????*/write_sock(client,":%s 004 %s %s %s iswo bkntmsk\r\n",SERVER_NAME,client->nick,SERVER_NAME,FREEIRCD_NAME);


        show_version(client);

        show_lusers(client,users,ircops,channels);

        show_motd(client,motd);

        /*:orium!~orium@irc.ptsida.org MODE orium +i*/
        write_sock(client,":%s!%s@%s MODE %s +i\r\n",client->nick,USER_NAME,client->md5_ip,client->nick);

        client->do_welcome=0;
}

void show_version(CLIENTS *client)
{
        write_sock(client,":%s 005 %s NICKLEN=%d TOPICLEN=4000 KICKLEN=4000 CHANTYPES=# PREFIX=(ov)@+ CASEMAPPING=rfc1459 NETWORK=%s "
                          "CHANMODES=b,k,ntmsk "
                          ":are supported by this server\r\n",SERVER_NAME,client->nick,MAX_NICKNAME_LEN,NETWORK_NAME);
}

void show_lusers(CLIENTS *client, long users, long ircops, long channels)
{
/*
:server 251 nick :There are 0 users and 3 invisible on 1 servers
:server 252 nick 1 :operator(s) online
:server 254 nick 5 :channels formed
:server 255 nick :I have 3 clients and 0 servers
*/
        write_sock(client,":%s 251 %s :There %s %ld user%s and %ld invisible on 1 server\r\n",SERVER_NAME,client->nick,
                                                                                               (users == 1) ? "is" : "are",users,
                                                                                               (users == 1) ? "" : "s",users);
        write_sock(client,":%s 252 %s %ld :operator%s online\r\n",SERVER_NAME,client->nick,ircops,(ircops == 1) ? "" : "s");
        write_sock(client,":%s 254 %s %ld :channel%s formed\r\n",SERVER_NAME,client->nick,channels,(channels == 1) ? "" : "s");
        write_sock(client,":%s 255 %s :I have %ld client%s and 1 server\r\n",SERVER_NAME,client->nick,users,(users == 1) ? "" : "s");
}


void show_motd(CLIENTS *client, MOTD *motd)
{
/*
:server 375 nick :- irc.ptsida.org Message of the day
:server 372 nick :- ...
:server 376 nick :End of /MOTD command.
*/
        if (motd == NULL)
        {
                write_sock(client,":%s 422 %s :MOTD File is missing\r\n",SERVER_NAME,client->nick);
                return;
        }

        write_sock(client,":%s 375 %s :- %s Message of the day\r\n",SERVER_NAME,client->nick,SERVER_NAME);


        while (motd != NULL)
        {
                write_sock(client,":%s 375 %s :- %s\r\n",SERVER_NAME,client->nick,motd->line);
                motd=motd->next;
        }

        write_sock(client,":%s 376 %s :End of /MOTD command.\r\n",SERVER_NAME,client->nick,SERVER_NAME);
}


void info(CLIENTS *client)
{
        write_sock(client,":%s 371 %s :%s version %s\r\n",SERVER_NAME,client->nick,FREEIRCD_NAME,FREEIRCD_VERSION);
        write_sock(client,":%s 371 %s :Written by Diogo Sousa aka orium\r\n",SERVER_NAME,client->nick);
        write_sock(client,":%s 371 %s :Compiled %s at %s with gcc %s\r\n",SERVER_NAME,client->nick,__DATE__,__TIME__,__VERSION__);
        write_sock(client,":%s 371 %s :End of /INFO list.\r\n",SERVER_NAME,client->nick);
}

